package views;

import info.clearthought.layout.TableLayout;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import controllers.MainController;

public class MainWindow extends JFrame
{
	/**
	 * Generated serial ID
	 */
	private static final long	serialVersionUID	= 2114730795472956572L;
	
	private MainController		mainController		= null;
	
	private MainWindowMenuBar	menuBar				= null;

	private JScrollPane			scrollContentPanel	= null;
	private JPanel				contentPanel		= null;
	private ControlPanel		controlPanel		= null;
	private Canvas				canvas				= null;
	
	/**
	 * ################### # Constants # ###################
	 */

	public static int		WIDTH					= 0;
	public static int		HEIGHT					= 0;
	
	/**
	 * ################## # End of vars # ##################
	 */
	
	public MainWindow(MainController mainController)
	{
		this.mainController = mainController;
		
		Toolkit t = Toolkit.getDefaultToolkit(); 
        Dimension d = t.getScreenSize();

        WIDTH = (int) Math.floor(d.getWidth() * 0.90f);
        HEIGHT = (int) Math.floor(d.getHeight() * 0.75f);
        
		setSize(WIDTH, HEIGHT);
		setLocation(new Point((d.width / 2) - (WIDTH / 2), 
                        (d.height / 2) - (HEIGHT / 2) + 15));
		
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		setTitle("AttractorGen");
		
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (ClassNotFoundException e) { System.err.println(e.getMessage()); }
		catch (InstantiationException e) { System.err.println(e.getMessage()); }
		catch (IllegalAccessException e) { System.err.println(e.getMessage()); }
		catch (UnsupportedLookAndFeelException e) { System.err.println(e.getMessage()); }

		setJMenuBar(getMB());		
		setContentPane(getContentPanel());

		setVisible(true);
	}
	
	private JPanel getContentPanel()
	{
		if (contentPanel == null)
		{
			contentPanel = new JPanel();
			
			double size[][] =
			{
							{ Canvas.WIDTH, 5, (WIDTH - Canvas.WIDTH) - 15, 10 },
							{ 5, HEIGHT - 55, 5 }
			};
			
			contentPanel.setLayout(new TableLayout(size));
			contentPanel.add(getCanvas(), "0, 0, 0, 1");
			contentPanel.add(getScrollCP(), "2, 1, 2, 1");
		}
		
		return contentPanel;
	}
	
	private JScrollPane getScrollCP()
	{
		if (scrollContentPanel == null)
		{
			scrollContentPanel = new JScrollPane(getCP());
			scrollContentPanel.setBorder(BorderFactory.createEmptyBorder());
			scrollContentPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		}
		
		return scrollContentPanel;
	}
	
	public ControlPanel getCP()
	{
		if (controlPanel == null)
		{
			controlPanel = new ControlPanel(mainController);
		}
		
		return controlPanel;
	}
	
	private Canvas getCanvas()
	{
		if (canvas == null)
		{
			canvas = new Canvas(mainController);
		}
		
		return canvas;
	}
	
	private MainWindowMenuBar getMB()
	{
		if (menuBar == null)
		{
			menuBar = new MainWindowMenuBar(mainController);
		}
		
		return menuBar;
	}
	
	public void run()
	{
		getCanvas().init();
	}
}
